package com.winphone.zhgd.web.ls;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winphone.zhgd.entity.TbLsWorker;
import com.winphone.zhgd.entity.User;
import com.winphone.zhgd.service.ITbLsWorkerService;
import com.winphone.zhgd.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  劳务模块 前端控制器
 * </p>
 *
 * @author zhou
 * @since 2017-08-30
 */
@Controller
@RequestMapping("/tbLsWorker")
public class TbLsWorkerController  {

    @Autowired
    private ITbLsWorkerService ws;


    /**
     * 分页 PAGE
     */
    @RequestMapping("/all")
    public Page<TbLsWorker> test() {
        return ws.selectPage(new Page<TbLsWorker>(0, 12));
    }

    @RequestMapping("/allParam")
    public String testPage(TbLsWorker tbLsWorker,
                           @RequestParam(value = "current",required = false) Integer current,
                           @RequestParam(value = "size",required = false) Integer size, Model model){
        Wrapper<TbLsWorker> workerWrapper = new EntityWrapper<>(tbLsWorker);
        if (null == current){
            current = 0;
        }
        if (null == size){
            size = 10;
        }
        Page<TbLsWorker> page = ws.selectPage(new Page<>(current,size),workerWrapper);
        model.addAttribute("page",page);
        return  "modules/sys/userList";
    }

    @RequestMapping("/allParam1")
    @ResponseBody
    public Page testPage1(TbLsWorker tbLsWorker,
                           @RequestParam(value = "current",required = false) Integer current,
                           @RequestParam(value = "size",required = false) Integer size, Model model){
        Wrapper<TbLsWorker> workerWrapper = new EntityWrapper<>(tbLsWorker);
        if (null == current){
            current = 0;
        }
        if (null == size){
            size = 10;
        }
        Page<TbLsWorker> page = ws.selectPage(new Page<>(current,size),workerWrapper);

        return page;
    }

    @RequestMapping(value = "/save")
    public Object saveTbLsWorker(@Valid @ModelAttribute("tbLsWorker") TbLsWorker tbLsWorker,BindingResult result){
        if (result.hasErrors()){
            return result.getAllErrors().get(0).getDefaultMessage();
        }
        return ws.insert(tbLsWorker);
    }

    @RequestMapping(value = "/deleteWorker")
    public boolean deleteTbLsWorker(@RequestParam(value = "workid") Integer workid){
       return ws.deleteById(workid);
    }

    @RequestMapping(value = "/get")
    public Object findTbLsWorker(@RequestParam(value = "workid") Integer workid){
        Map<String,Object> tbLsWorkers = new HashMap<>();
        tbLsWorkers.put("workid",workid);

        List<TbLsWorker> tbLsWorkers1 = ws.selectByMap(tbLsWorkers);
        return tbLsWorkers1;
    }

    @RequestMapping("/cd")
    public Object ss(Long id){
        return  ws.get(id);
    }

}
